/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.canvaschart.client.background;

import com.google.gwt.core.client.GWT;
import com.google.gwt.widgetideas.graphics.client.Color;

import net.kornr.abstractcanvas.client.CanvasTextSupport;
import net.kornr.abstractcanvas.client.ICanvasExt;
import net.kornr.abstractcanvas.client.Rectangle;
import net.kornr.abstractcanvas.client.TextMetrics;
import net.kornr.canvaschart.client.core.Background;
import net.kornr.canvaschart.client.core.CoordinateSystem;
import net.kornr.canvaschart.client.core.Formatter;
import net.kornr.canvaschart.client.core.Point;
import net.kornr.canvaschart.client.formatter.NumberFormatter;

public class BackgroundGrid implements Background 
{
	private int m_xgrid = 10;
	private int m_ygrid = 10;
	private double m_xLineWidth = 1.0;
	private double m_yLineWidth = 0.7;

	private String m_fontname = "Serif";
	private int    m_fontsize = 18;
	
	private String m_color = "#AAAAAA";
	private Formatter m_yformatter = new NumberFormatter();
	private Formatter m_xformatter = new NumberFormatter();

	public BackgroundGrid()
	{
	}

	public BackgroundGrid(int xgrid, int ygrid)
	{
		m_xgrid = xgrid;
		m_ygrid = ygrid;
	}

	public BackgroundGrid(int xgrid, int ygrid, int xlinewidth, int ylineheight)
	{
		m_xgrid = xgrid;
		m_ygrid = ygrid;
		m_xLineWidth = xlinewidth;
		m_yLineWidth = ylineheight;
	}

	public void drawBackground(ICanvasExt canvas, CoordinateSystem coords) 
	{
		double total = coords.getCoordMaxX()-coords.getCoordMinX();
		double offset = coords.getCoordMinX();
		canvas.setFontName(m_fontname);
		canvas.setFontSize(m_fontsize);
		
		double maxylabelwidth = Double.MIN_VALUE;
		for (int i=0; i<m_ygrid; i++)
		{
			if (m_yformatter != null)
			{
				String s = m_xformatter.format(offset);
				TextMetrics tm = canvas.measureText(s);
				if (tm.getWidth() > maxylabelwidth)
					maxylabelwidth = tm.getWidth();
				offset += total/m_ygrid;
			}
		}

		offset = coords.getCoordMinX();
		double maxxlabelheight = Double.MIN_VALUE;
		for (int i=0; i<m_xgrid; i++, offset+= total/m_xgrid)
		{
			offset = total*i / m_xgrid;
			if (m_xformatter != null)
			{
				String s = m_xformatter.format(offset);
				TextMetrics tm = canvas.measureText(s);
				if (tm.getHeight() > maxxlabelheight)
				{
					maxxlabelheight = tm.getHeight();
				}
			}
		}
		
		// GWT.log("offsetx="+maxylabelwidth+", offsetbottom="+maxxlabelheight, null);
		coords.setCanvasOffsets(maxylabelwidth+10,0,canvas.getCoordWidth()-maxylabelwidth-10, canvas.getCoordHeight()-maxxlabelheight);		

		canvas.setFillStyle(new Color(m_color));
		Point bottom = coords.getBottomRight();
	
		canvas.setLineWidth(m_xLineWidth);
		canvas.setStrokeStyle(Color.BLACK);
		canvas.setAlign(CanvasTextSupport.ALIGN_CENTER);
		
		offset = coords.getCoordMinX();
		for (int i=0; i<m_xgrid; i++, offset+= total/m_xgrid)
		{
			offset = total*i / m_xgrid;
			
			canvas.beginPath();
			Point a = coords.getCoord(offset, coords.getCoordMaxY());
			Point b = coords.getCoord(offset, coords.getCoordMinY());
			canvas.moveTo(a.getX(), a.getY());
			canvas.lineTo(b.getX(), b.getY());
			canvas.stroke();
			if (m_xformatter != null)
				canvas.strokeText(m_xformatter.format(offset), (int)b.getX(), (int)bottom.getY()+2);
		}

		canvas.setLineWidth(m_yLineWidth);
		canvas.setAlign(CanvasTextSupport.ALIGN_END);
		total = coords.getCoordMaxY()-coords.getCoordMinY();
		offset = coords.getCoordMinY();
		for (int i=0; i<m_ygrid; i++)
		{
			canvas.beginPath();
			Point a = coords.getCoord(coords.getCoordMaxX(), offset);
			Point b = coords.getCoord(coords.getCoordMinX(), offset);
			canvas.moveTo(a.getX(), a.getY());
			canvas.lineTo(b.getX(), b.getY());
			canvas.stroke();
			if (m_yformatter != null)
				canvas.strokeText(m_yformatter.format(offset), (int)b.getX()-2, (int)b.getY() - m_fontsize);
			offset += total/m_ygrid;
		}
	}

	public int getXgrid() {
		return m_xgrid;
	}

	public void setXgrid(int xgrid) {
		this.m_xgrid = xgrid;
	}

	public int getYgrid() {
		return m_ygrid;
	}

	public void setYgrid(int ygrid) {
		this.m_ygrid = ygrid;
	}

	public double getXLineWidth() {
		return m_xLineWidth;
	}

	public void setXLineWidth(double lineWidth) {
		m_xLineWidth = lineWidth;
	}

	public double getYLineWidth() {
		return m_yLineWidth;
	}

	public void setYLineWidth(double lineWidth) {
		m_yLineWidth = lineWidth;
	}

	public String getColor() {
		return m_color;
	}

	public void setColor(String color) {
		this.m_color = color;
	}

	public Formatter getYformatter() {
		return m_yformatter;
	}

	public void setYformatter(Formatter yformatter) {
		this.m_yformatter = yformatter;
	}

	public Formatter getXformatter() {
		return m_xformatter;
	}

	public void setXformatter(Formatter xformatter) {
		this.m_xformatter = xformatter;
	}

}
